package com.starlight.easybuyprictice.service;

import com.starlight.easybuyprictice.entity.MsGoods;
import com.starlight.easybuyprictice.mapper.GoodsCategoryMapper;
import com.starlight.easybuyprictice.mapper.MsGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class GoodsService {
    @Autowired
    private MsGoodsMapper msGoodsMapper;

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    public GoodsService setGoodsDao(MsGoodsMapper msGoodsMapper){
        this.msGoodsMapper=msGoodsMapper;
        return this;
    }

    public Result findGoodById(int goodsId){
        Result result =new Result();
        result.setData(msGoodsMapper.selectByPrimaryKey(goodsId));
        return result;
    }

    public Result listGoods(){
        Result result=new Result();
        List<MsGoods> goodsList=msGoodsMapper.selectAllGoods();
        result.setData(goodsList);
        return result;
    }

    public Result listGoodsRandom(int count){
        Result result=new Result();
        List<MsGoods> goodsList=msGoodsMapper.selectGoodsRandom(count);
        result.setData(goodsList);
        return result;
    }

    public Result listSameCatGoods(Integer catId) {
        Result result=new Result();
        List<MsGoods> goodsList=msGoodsMapper.selectSameCatGoods(catId);
        result.setData(goodsList);
        return result;
    }

    public Result addGood(String name,int cat_id,BigDecimal price,int count,String description){
        MsGoods goods = new MsGoods();
        Result result = new Result();
        goods.setCatId(cat_id);
        goods.setName(name);
        goods.setCount(count);
        goods.setDescription(description);
        goods.setCreatetime(new Date());
        goods.setModifytime(new Date());
        goods.setPrice(price);
        if(msGoodsMapper.insertSelective(goods)==1){
            result.setStatus(0);
            result.setMsg("添加成功");
            //商品添加后，修改分类下商品数加一
//            goodsCategoryMapper.add
            //设置result.data为新增加的商品的id
            result.setData(msGoodsMapper.selectLastInsertId());
        }
        else {
            result.setStatus(2);
            result.setMsg("添加失败");
        }
        return result;
    }

    public Result updateGood(Integer id, String name, int catId, BigDecimal price, int count, String description) {
        MsGoods goods = new MsGoods();
        Result result = new Result();
        goods.setGoodsId(id);
        goods.setCatId(catId);
        goods.setName(name);
        goods.setCount(count);
        goods.setDescription(description);
        goods.setModifytime(new Date());
        goods.setPrice(price);
        if(msGoodsMapper.updateByPrimaryKeySelective(goods)==1){
            result.setStatus(0);
            result.setMsg("修改成功");
        }
        else {
            result.setStatus(2);
            result.setMsg("修改失败");
        }
        return result;
    }

    public Result deleteGoodById(Integer id){
        Result result = new Result();
        if(msGoodsMapper.deleteByPrimaryKey(id)==1){
            result.setStatus(0);
            result.setMsg("删除成功");
        }
        else {
            result.setStatus(2);
            result.setMsg("删除失败");
        }
        return result;
    }
}
